﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyRadioButton
{
    public partial class MyRadioButton : RadioButton
    {
        public MyRadioButton()
        {
            InitializeComponent();
        }

        protected Image checked_h = (Image)Properties.Resources.checked_h;
        [Category("自定义"), Description("选中高亮")]
        [Browsable(true)]
        public Image Checked_h
        {
            get
            {
                return checked_h;
            }
            set
            {
                checked_h = value;
            }
        }

        protected Image checked_n = (Image)Properties.Resources.checked_n;
        [Category("自定义"), Description("选中高亮")]
        [Browsable(true)]
        public Image Checked_n
        {
            get
            {
                return checked_n;
            }
            set
            {
                checked_n = value;
            }
        }


        protected Image notchecked_h = (Image)Properties.Resources.notchecked_h;
        [Category("自定义"), Description("选中高亮")]
        [Browsable(true)]
        public Image NotChecked_h
        {
            get
            {
                return notchecked_h;
            }
            set
            {
                notchecked_h = value;
            }
        }

        protected Image notchecked_n = (Image)Properties.Resources.notchecked_n;
        [Category("自定义"), Description("选中高亮")]
        [Browsable(true)]
        public Image NotChecked_n
        {
            get
            {
                return notchecked_n;
            }
            set
            {
                notchecked_n = value;
            }
        }

        public enum State
        {
            checked_h,
            checked_n,
            notchecked_h,
            notchecked_n
        }
        private State buttonState = State.notchecked_n;
        protected override void OnMouseEnter(EventArgs e)
        {
            if(buttonState == State.checked_n)
                buttonState = State.checked_h;
            if (buttonState == State.notchecked_n)
                buttonState = State.notchecked_h;
            base.OnMouseEnter(e);
            Invalidate();
        }

        private bool isChecked = false;

        protected override void OnMouseLeave(EventArgs e)
        {
            if (buttonState == State.checked_h)
                buttonState = State.checked_n;
            if (buttonState == State.notchecked_h)
                buttonState = State.notchecked_n;
            base.OnMouseLeave(e);
            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            buttonState = State.checked_n;
            isChecked = true;
            base.OnMouseDown(mevent);
            Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            if (ClientRectangle.Contains(mevent.X, mevent.Y))
            {
                buttonState = State.checked_h;
            }
            else
            {
                buttonState = State.checked_n;
            }
            base.OnMouseUp(mevent);
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            base.OnPaintBackground(pevent);

            Graphics g = pevent.Graphics;
            if (Checked)
            {
                switch (buttonState)
                {
                    case State.checked_h:
                        g.DrawImage(checked_h, new Rectangle(0, 0, checked_h.Width, checked_h.Height));
                        break;
                    default:
                        g.DrawImage(checked_n, new Rectangle(0, 0, checked_n.Width, checked_n.Height));
                        break;
                }
            }
            else
            {
                switch (buttonState)
                {
                    case State.notchecked_h:
                        g.DrawImage(notchecked_h, new Rectangle(0, 0, notchecked_h.Width, notchecked_h.Height));
                        break;
                    default:
                        g.DrawImage(notchecked_n, new Rectangle(0, 0, notchecked_h.Width, notchecked_h.Height));
                        break;
                }
            }
            g.DrawString(this.Text, this.Font, new SolidBrush(Color.White), new Point(checked_h.Width + 3, 0));
            Console.WriteLine(buttonState + " in MyRadioButton");
        }
    }
}
